home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 1 / Gold Medal Software Volume 1 (Gold Medal) (1994).iso / prog / objeng.arj / SAMPLE.ARJ / TEST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-08  |  9.0 KB  |  294 lines

  1. /****************************************************************************
  2.  * Generated by OEGen.
  3.  * Comments.......  This file contains the Database Mgmt. code specific to
  4.  *                  the descendents of a persistant class.  This code is
  5.  *                  generated automatically.
  6.  *
  7.  ***************************************************************************/
  8.  
  9. #include "C:\FITDEV\DATABASE\TEST.HPP"
  10. #ifdef _Windows
  11. ObjEng objeng("OEApp");
  12. #else
  13. ObjEng objeng;
  14. #endif
  15.  
  16.  
  17. //------------------------------------------------------------
  18. // The Client Class Constructor
  19. //------------------------------------------------------------
  20. Client::Client() : PersistClass("client")
  21. {
  22.       id = 0;
  23.       strcpy(fname, "");
  24.       tests = new OEList;
  25.     strcpy(localtypes, "LA^L");
  26.  
  27.     if (!objeng.IsRegistered(oehandle))
  28.     {
  29.         char *FieldNames[] = {"ID", "FNAME", "TESTS", "ObjectID"};
  30.         char *FieldTypes[] = {"N", "A12", "N", "N"};
  31.         int nFields = 4;
  32.         FIELDHANDLE IdxFlds[] = { 1 };
  33.         int nIdxFields = 1;
  34.         if ((status = objeng.Register(oehandle, FieldNames, FieldTypes, IdxFlds, nFields, nIdxFields)) != PXSUCCESS) return;
  35.     }
  36.     // ANY ADDITIONAL CONSTRUCTOR CODE CAN BE PLACED HERE...
  37. }
  38.  
  39.  
  40.  
  41. //------------------------------------------------------------
  42. // Copy contents from one client to another - DO NOT link them.
  43. // (objectID is not copied so the copy doesn't refer to DB record).
  44. //------------------------------------------------------------
  45. Client &Client::operator=(const Client &val)
  46. {
  47.       id = val.id;
  48.       strcpy(fname, val.fname);
  49.       tests = val.tests;
  50.     if (tests != NULL)
  51.         tests->AddReference();   //Increment refcount to avoid unwanted deletion
  52.     strcpy(localtypes, "LA^L");
  53.  
  54.     // Base class copies...
  55.     Copy((PersistClass &)val);
  56.     return *this;
  57. }
  58.  
  59.  
  60. //------------------------------------------------------------
  61. // Link to the record matching on the given number of index fields.
  62. //------------------------------------------------------------
  63. int Client::LinkToKey(int nIdxFields, int mode)
  64. {
  65.     //Copy all of the Primary index fields to the recbuf 
  66.     PXPutLong(oehandle.rHdl, 1, id);
  67.     // Do the SrchKey on just those that are asked for (nIdxFields).
  68.     if ((status = PXSrchKey(oehandle.tHdl, oehandle.rHdl, nIdxFields, mode)) == PXSUCCESS) 
  69.         return Retrieve();
  70.     else
  71.         return status;
  72. }
  73.  
  74.  
  75. //------------------------------------------------------------
  76. // Store a filled/changed object - create an indexID if virgin,
  77. // otherwise, store under the objectID.
  78. //------------------------------------------------------------
  79. int Client::Store()
  80. {
  81.     PXPutLong(oehandle.rHdl, 1, id);
  82.     PXPutAlpha(oehandle.rHdl, 2, fname);
  83.     long listid0 = tests->GetListID();
  84.     PXPutLong(oehandle.rHdl, 3, listid0);
  85.     if (objectID == 0) // Not yet added, give Index, then add
  86.     {
  87.         if ((status = objeng.GetIndex(objectID, 0)) != PXSUCCESS) return status;
  88.         PXPutLong(oehandle.rHdl, 4, objectID);
  89.         status = PXRecInsert(oehandle.tHdl, oehandle.rHdl);
  90.     }
  91.     else  // 'Tis an update.
  92.     {
  93.         PXPutLong(oehandle.rHdl, 4, objectID);
  94.         status = PXSrchFld(oehandle.tHdl, oehandle.rHdl, 4, SEARCHFIRST);
  95.         
  96.         if (status == PXSUCCESS)
  97.             status = PXRecUpdate(oehandle.tHdl, oehandle.rHdl);
  98.         else
  99.         {
  100.             // If you want a missing object to be an error, then
  101.             // uncomment THIS line....
  102.             // status = PXERR_RECNOTFOUND;
  103.             // and comment out THIS line....
  104.             status = PXRecInsert(oehandle.tHdl, oehandle.rHdl);
  105.         }
  106.     }
  107.     return status;
  108. }
  109.  
  110. //------------------------------------------------------------
  111. // An Object Retrieve Function - Private! Should only be called by 
  112. // one of the linking functions (Link..., First, Last, ++, --)
  113. //------------------------------------------------------------
  114. int Client::Retrieve()
  115. {
  116.     // Free up memory currently allocated to OEList if OEList
  117.     // reference count is 0.
  118.     if ((tests != NULL) && (tests->DeleteOK() == TRUE)) delete tests;
  119.     if ((status = PXRecGet(oehandle.tHdl, oehandle.rHdl)) == PXSUCCESS)
  120.     {
  121.         PXGetLong(oehandle.rHdl, 1, &id);
  122.         PXGetAlpha(oehandle.rHdl, 2, 13, fname);
  123.         long listid0;
  124.         PXGetLong(oehandle.rHdl, 3, &listid0);
  125.         tests = new OEList(listid0);
  126.         PXGetLong(oehandle.rHdl, 4, &objectID);
  127.     }
  128.     return status;
  129. }
  130.  
  131.  
  132. //------------------------------------------------------------
  133. // The Test Class Constructor
  134. //------------------------------------------------------------
  135. Test::Test() : PersistClass("test")
  136. {
  137.       id = 0;
  138.       uid = 0;
  139.       weight = 0.0;
  140.       score = 0;
  141.       date.tm_mday = 0;
  142.     date.tm_mon = 0;
  143.     date.tm_year = 0;
  144.       height = 0.0;
  145.     strcpy(localtypes, "LL$SD$L");
  146.  
  147.     if (!objeng.IsRegistered(oehandle))
  148.     {
  149.         char *FieldNames[] = {"ID", "UID", "WEIGHT", "SCORE", "DATE", "HEIGHT", "ObjectID"};
  150.         char *FieldTypes[] = {"N", "N", "$", "S", "D", "$", "N"};
  151.         int nFields = 7;
  152.         FIELDHANDLE IdxFlds[] = { 1, 2 };
  153.         int nIdxFields = 2;
  154.         if ((status = objeng.Register(oehandle, FieldNames, FieldTypes, IdxFlds, nFields, nIdxFields)) != PXSUCCESS) return;
  155.     }
  156.     // ANY ADDITIONAL CONSTRUCTOR CODE CAN BE PLACED HERE...
  157. }
  158.  
  159.  
  160.  
  161. //------------------------------------------------------------
  162. // Copy contents from one client to another - DO NOT link them.
  163. // (objectID is not copied so the copy doesn't refer to DB record).
  164. //------------------------------------------------------------
  165. Test &Test::operator=(const Test &val)
  166. {
  167.       id = val.id;
  168.       uid = val.uid;
  169.       weight = val.weight;
  170.       score = val.score;
  171.       date.tm_mday = val.date.tm_mday;
  172.     date.tm_mon = val.date.tm_mon;
  173.     date.tm_year = val.date.tm_year;
  174.       height = val.height;
  175.     strcpy(localtypes, "LL$SD$L");
  176.  
  177.     // Base class copies...
  178.     Copy((PersistClass &)val);
  179.     return *this;
  180. }
  181.  
  182.  
  183. //------------------------------------------------------------
  184. // Link to the record matching on the given number of index fields.
  185. //------------------------------------------------------------
  186. int Test::LinkToKey(int nIdxFields, int mode)
  187. {
  188.     //Copy all of the Primary index fields to the recbuf 
  189.     PXPutLong(oehandle.rHdl, 1, id);
  190.     PXPutLong(oehandle.rHdl, 2, uid);
  191.     // Do the SrchKey on just those that are asked for (nIdxFields).
  192.     if ((status = PXSrchKey(oehandle.tHdl, oehandle.rHdl, nIdxFields, mode)) == PXSUCCESS) 
  193.         return Retrieve();
  194.     else
  195.         return status;
  196. }
  197.  
  198.  
  199. //------------------------------------------------------------
  200. // Store a filled/changed object - create an indexID if virgin,
  201. // otherwise, store under the objectID.
  202. //------------------------------------------------------------
  203. int Test::Store()
  204. {
  205.     PXPutLong(oehandle.rHdl, 1, id);
  206.     PXPutLong(oehandle.rHdl, 2, uid);
  207.     PXPutDoub(oehandle.rHdl, 3, weight);
  208.     PXPutShort(oehandle.rHdl, 4, score);
  209.     OEPutDate(5, date);
  210.     PXPutDoub(oehandle.rHdl, 6, height);
  211.     if (objectID == 0) // Not yet added, give Index, then add
  212.     {
  213.         if ((status = objeng.GetIndex(objectID, 0)) != PXSUCCESS) return status;
  214.         PXPutLong(oehandle.rHdl, 7, objectID);
  215.         status = PXRecInsert(oehandle.tHdl, oehandle.rHdl);
  216.     }
  217.     else  // 'Tis an update.
  218.     {
  219.         PXPutLong(oehandle.rHdl, 7, objectID);
  220.         status = PXSrchFld(oehandle.tHdl, oehandle.rHdl, 7, SEARCHFIRST);
  221.         
  222.         if (status == PXSUCCESS)
  223.             status = PXRecUpdate(oehandle.tHdl, oehandle.rHdl);
  224.         else
  225.         {
  226.             // If you want a missing object to be an error, then
  227.             // uncomment THIS line....
  228.             // status = PXERR_RECNOTFOUND;
  229.             // and comment out THIS line....
  230.             status = PXRecInsert(oehandle.tHdl, oehandle.rHdl);
  231.         }
  232.     }
  233.     return status;
  234. }
  235.  
  236. //------------------------------------------------------------
  237. // An Object Retrieve Function - Private! Should only be called by 
  238. // one of the linking functions (Link..., First, Last, ++, --)
  239. //------------------------------------------------------------
  240. int Test::Retrieve()
  241. {
  242.     // Free up memory currently allocated to OEList if OEList
  243.     // reference count is 0.
  244.     if ((status = PXRecGet(oehandle.tHdl, oehandle.rHdl)) == PXSUCCESS)
  245.     {
  246.         PXGetLong(oehandle.rHdl, 1, &id);
  247.         PXGetLong(oehandle.rHdl, 2, &uid);
  248.         double temp1;
  249.         PXGetDoub(oehandle.rHdl, 3, (double *)&temp1);
  250.         weight = temp1;
  251.         PXGetShort(oehandle.rHdl, 4, &score);
  252.         OEGetDate(5, date);
  253.         double temp2;
  254.         PXGetDoub(oehandle.rHdl, 6, (double *)&temp2);
  255.         height = temp2;
  256.         PXGetLong(oehandle.rHdl, 7, &objectID);
  257.     }
  258.     return status;
  259. }
  260.  
  261.  
  262. //--------------------------------------------------------------------
  263. // This "global" function provides the mapping from table names
  264. // to table objects for the automatic "has-a" link storage/retrieval.
  265. // It is always generated last since it needs to summarize all of the
  266. // classes defined above.
  267. //--------------------------------------------------------------------
  268. PersistClass *GetPersistClass(char *tbl, const long &inID)
  269. {
  270.     PersistClass* tmp;
  271.  
  272.     if (strcmp(tbl, "client") == 0)
  273.     {
  274.         Client *client = new Client();
  275.         tmp = (PersistClass *)client;
  276.     }
  277.     else
  278.     if (strcmp(tbl, "test") == 0)
  279.     {
  280.         Test *test = new Test();
  281.         tmp = (PersistClass *)test;
  282.     }
  283.     else
  284.         return NULL;
  285.  
  286.     // Common calls for all Persistant Classes.
  287.     if ((tmp->GetStatus() != PXSUCCESS) || (tmp->LinkToID(inID) != PXSUCCESS))
  288.     {
  289.         delete tmp;
  290.         return NULL;
  291.     }
  292.     return(tmp);
  293. }
  294.